using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QuanLyHocPhi.DAL;
using QuanLyHocPhi.Entity;
using QuanLyHocPhi.Util;

namespace QuanLyHocPhi
{
    public partial class frmThongkechuadonghp : DevComponents.DotNetBar.Metro.MetroForm
    {
        LOP_HOC_DAO daoLop = new LOP_HOC_DAO();
        HOA_DON_DAO daoHoaDon = new HOA_DON_DAO();
        HOA_DON_CT_DAO daoHoaDonCT = new HOA_DON_CT_DAO();
        IList<LOP_HOC> dsLopHoc;
        IList<HOA_DON> dsHoaDon;
        HOA_DON hoaDonHienTai;
        bool isInit = false;
        public frmThongkechuadonghp()
        {
            InitializeComponent();

        }

        private void frmThongkechuadonghp_Load(object sender, EventArgs e)
        {
            isInit = true;
            dsLopHoc = daoLop.GetAll("%");
            cbbLop.DataSource = dsLopHoc;
            cbbLop.DisplayMember = "TEN_LOP_HOC";
            cbbLop.ValueMember = "MA_LOP_HOC";
            cbbLop.SelectedIndex = -1;
            isInit = false;
          
        }

        private void dgvHoaDon_CellClick(object sender, DataGridViewCellEventArgs e)
        {

            int row = dgvHoaDon.CurrentRow.Index ;
            if (row < 0 || row >=dgvHoaDon.Rows.Count)
            {
                return;
            }
            HOA_DON inv = dsHoaDon[row];
            hoaDonHienTai = inv;
            txtTenSV.Text = inv.SINH_VIEN.TEN_SV;
            dpkNgaySinh.Value=inv.SINH_VIEN.NGAY_SINH;
            txtDiaChi.Text=inv.SINH_VIEN.DIA_CHI;
            txtEmail.Text = inv.SINH_VIEN.EMAIL;
            txtSoDT.Text=inv.SINH_VIEN.SO_DIEN_THOAI;
            cbbLop.Text=inv.MA_LOP_HOC.TEN_LOP_HOC;
            dpkNgayKichHoat.Value=inv.NGAY_KICH_HOAT;

            //xu li ngay thu phi
            IList<HOA_DON_CT> dsChiTietHoaDon=daoHoaDonCT.GetSubInvoicesByInvoice(hoaDonHienTai.MA_HOA_DON);
            float soTienThanhToan=0;
            foreach(HOA_DON_CT ct in dsChiTietHoaDon){
                
                    
                if(!ct.TRANG_THAI){
                    dpkNgayThuPhi.Value=ct.NGAY_THU_PHI;
                    break;
                }else{
                    soTienThanhToan+=ct.SO_TIEN;
                }
            }
           
            txtSoNgayQuaHan.Text=DateTime.Now.Subtract(dpkNgayThuPhi.Value).Days.ToString();
            txtTongTien.Text = hoaDonHienTai.MA_MUC_PHI.TONG_TIEN.ToString();
            txtSoLanThanhToan.Text = hoaDonHienTai.MA_MUC_PHI.SO_LAN_THANH_TOAN.ToString();
            txtThanhToanHienTai.Text = soTienThanhToan.ToString();
            txtPhiPhaiTra.Text = (hoaDonHienTai.MA_MUC_PHI.TONG_TIEN - soTienThanhToan).ToString();
            txtGhiChu.Text = hoaDonHienTai.GHI_CHU;

        }

        private void cbbLop_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!isInit)
            {
                int err = 0;
                int overdayNum = KieuDuLieu.ChuyenSangsonguyen(txtQuaHan.Text, out err);
                LOP_HOC lop = new LOP_HOC();
                if (cbbLop.SelectedIndex > -1)
                {
                    lop=dsLopHoc[cbbLop.SelectedIndex];
                }

               
                //load data to table
                //  int overdays = 0;

                dsHoaDon = daoHoaDon.QueryInvoiceView(overdayNum, lop.MA_LOP_HOC);

                objectBindingSource1.DataSource = dsHoaDon;
                dgvHoaDon.AutoGenerateColumns = false;
                dgvHoaDon.AllowUserToAddRows = false;
                dgvHoaDon.AllowUserToDeleteRows = false;
                dgvHoaDon.DataSource = objectBindingSource1;
            }
        }

        private void btnKiemTra_Click(object sender, EventArgs e)
        {
            cbbLop_SelectedIndexChanged(sender, e);
        }
    }
}